package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.DeleteDeptBo;
import com.zhuss.bean.vo.core.company.DetailDeptVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.DeptDao;
import com.zhuss.core.db.entity.Dept;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 删除部门 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("deptDelete")
public class DeptDelete extends AbstractProcessor<Object, DeleteDeptBo> implements Processor<Object, DeleteDeptBo> {

    @Autowired
    private DeptDao deptDao;
    
    @Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
    
    @Autowired
	@Qualifier("userDeptRead")
	private UserDeptRead userDeptRead;
    
    
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(DeleteDeptBo bo) throws OceanException {
    	Long deptId = bo.getDeptId();
    	List<Long> userIdList = (List<Long>) userDeptRead.listUserId(deptId);
    	if (!userIdList.isEmpty()) {
    		throw new OceanException(ExceptionCode.不能删除有用户的部门);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteDeptBo param) throws OceanException {
    	Long deptId = param.getDeptId();
    	String name = ((DetailDeptVo) deptRead.detail(deptId)).getName();
    	
    	Dept record = new Dept();
    	record.setDeptId(deptId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	deptDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteDeptBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.部门删除.key());
    	record.setTargetId(param.getDeptId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
